package com.phone580.app.model;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.phone580.app.entity.UpdateEntity;

public class CheckUpdateModel extends BaseModel {


	public UpdateEntity parseCheckUpdateStep1(String xml) throws ParserConfigurationException, SAXException, IOException {
		Element root = getDomRootElement(xml);
		NodeList nodes = root.getElementsByTagName("package");
		if (nodes == null || nodes.getLength() == 0) {
			return null;
		}
		String versionCode = getAttrValue(root, "package", "versioncode");
		String urlRoot = getContent(root, "urlroot");
		String configFile = getContent(root, "configfile");
		UpdateEntity entity = new UpdateEntity();
		entity.setConfigFile(configFile);
		entity.setUrlRoot(urlRoot);
		entity.setVersionCode(versionCode);
		return entity;
	}
	
	public String parseCheckUpdateStep2(String xml) throws ParserConfigurationException, SAXException, IOException {
		Element root = getDomRootElement(xml);
		NodeList nodes = root.getElementsByTagName("File");
		if (nodes == null || nodes.getLength() == 0) {
			return null;
		}
		String name = getContent((Element)nodes.item(0), "name");
		return name;
	}
}
